package top.arryweb.application.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.arryweb.application.common.constant.KeyName;
import top.arryweb.application.common.exception.BusinessRuntimeException;
import top.arryweb.application.domain.dto.CreateTradeBlotterDTO;
import top.arryweb.application.domain.pojo.BookKeepPaymentOrder;
import top.arryweb.application.domain.pojo.BookKeepTradeBlotter;
import top.arryweb.application.mapper.BookKeepTradeBlotterMapper;
import top.arryweb.application.service.IBookKeepTradeBlotterService;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-05-27
 */
@Service
public class BookKeepTradeBlotterServiceImpl extends
    ServiceImpl<BookKeepTradeBlotterMapper, BookKeepTradeBlotter> implements
    IBookKeepTradeBlotterService {

  @Autowired
  private RedisTemplate<String, Object> redisTemplate;

  @Autowired(required = false)
  private BookKeepTradeBlotterMapper tradeBlotterMapper;

  /**
   * 获取交易流水ID
   */
  @Override
  public Long getTradeBlotterId(CreateTradeBlotterDTO createTradeBlotterDTO) {
    HashOperations<String, Long, BookKeepPaymentOrder> operations = redisTemplate.opsForHash();
    System.out.println(operations.get(KeyName.ENTITY_ORDER.getName(), createTradeBlotterDTO.getOrderId()));
    //获得实际支付的金额
    BigDecimal orderActualAmountPaid = Optional.ofNullable(
        operations.get(KeyName.ENTITY_ORDER.getName(), createTradeBlotterDTO.getOrderId()))
        .orElseThrow(() ->
            new BusinessRuntimeException("redis未找到相应的订单信息")).getActualAmountPaid();
    //单次的流水金额都不应该大于所属订单流水的实付金额
    if (orderActualAmountPaid.compareTo(createTradeBlotterDTO.getAmount()) < 0) {
      throw new BusinessRuntimeException("交易流水大于实际的订单流水实付金额，无法创建");
    }
    BookKeepTradeBlotter tradeBlotter = new BookKeepTradeBlotter(createTradeBlotterDTO);
    tradeBlotter.setVersion(0L);
    tradeBlotterMapper.insert(tradeBlotter);
    putRedis(tradeBlotter);
    return tradeBlotter.getId();
  }

  public void putRedis(BookKeepTradeBlotter tradeBlotter) {
    HashOperations<String, Long, BookKeepTradeBlotter> tradeBlotterRedis = redisTemplate
        .opsForHash();
    tradeBlotterRedis
        .put(KeyName.ENTITY_TRADE_BLOTTER.getName(), tradeBlotter.getId(), tradeBlotter);

    HashOperations<String, Long, List<Long>> operations = redisTemplate.opsForHash();
    List<Long> tradeIdList = Optional
        .ofNullable(operations.get(KeyName.IDS_TRADE_CAPITAL.getName(), tradeBlotter.getOrderId()))
        .orElse(new ArrayList<>());
    tradeIdList.add(tradeBlotter.getId());
    operations.put(KeyName.IDS_ORDER_TRADE.getName(), tradeBlotter.getOrderId(), tradeIdList);
    operations.put(KeyName.IDS_TRADE_CAPITAL.getName(), tradeBlotter.getId(), new ArrayList<>());
  }
}
